package com.tom.architect02.p4.day38.simple7.base;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tom.architect02.p4.day38.simple7.inject.InjectPresenter;
import com.tom.architect02.p4.day38.simple7.proxy.ActivityMvpProxy;
import com.tom.architect02.p4.day38.simple7.proxy.ActivityMvpProxyImpl;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Added by Tom on 2024/01/29. <BaseView>
 */
public abstract class BaseMvpActivity<P extends BasePresenter>
        extends AppCompatActivity implements BaseView {

    private P mPresenter; // 主的Presenter。处理一对一的情况(一个View 仅对应一个Presenter)
    private ActivityMvpProxy mMvpProxy;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
        // 创建P, 交给子类具体去时间。
//        mPresenter = createPresenter();
//        mPresenter.attach(this); // 绑定
        mMvpProxy = createMvpProxy();
        mMvpProxy.bindAndCreatePresenter(); // 执行注入

        initView();
        initData();
    }

    // 创建mvp 的代理，TODO：自己去模仿写 BaseFragment，ViewGroup的。
    protected ActivityMvpProxy createMvpProxy() {
        if (mMvpProxy == null) {
            mMvpProxy = new ActivityMvpProxyImpl<>(this);
        }
        return mMvpProxy;
    }

    // 子类去实现，每个Activity 实现不同。
    protected abstract P createPresenter();

    public P getPresenter() {
        return mPresenter;
    }

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void setContentView();

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMvpProxy.unbindPresenter(); // 解绑presenter
//        mPresenter.detach();
    }

}
